package com.hsjk.szwj.module.system.service.file;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.hsjk.szwj.framework.common.enums.OssSavePlaceEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.InputStream;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/08/17 22:56
 **/
@Service
@Slf4j
public class OssServiceImpl implements IOssService {

    @Value("${szwj.oss.aliyun-oss.endpoint}")
    private String endpoint;

    @Value("${szwj.oss.aliyun-oss.access-key-id}")
    private String accessKeyId;

    @Value("${szwj.oss.aliyun-oss.access-key-secret}")
    private String accessKeySecret;

    @Value("${szwj.oss.aliyun-oss.public-bucket-name}")
    private String publicBucketName;

    @Value("${szwj.oss.aliyun-oss.private-bucket-name}")
    private String privateBucketName;

    // ossClient 初始化
    private OSS ossClient = null;

    @PostConstruct
    public void init() {
        ossClient = new OSSClientBuilder().build(endpoint,
                accessKeyId,
                accessKeySecret);
    }

    @Override
    public String upload(OssSavePlaceEnum ossSavePlaceEnum,
                         MultipartFile multipartFile) {
        String fileName = System.currentTimeMillis() + "";
        String moduleName = "train";
        String saveFilePath = moduleName + "/" + fileName;
        try {
            InputStream is = multipartFile.getInputStream();
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentType("image/jpg");
            this.ossClient.putObject(new PutObjectRequest(ossSavePlaceEnum == OssSavePlaceEnum.PUBLIC
                    ? publicBucketName : privateBucketName, saveFilePath, is, meta));
            if (ossSavePlaceEnum == OssSavePlaceEnum.PUBLIC) {
                // 文档：https://www.alibabacloud.com/help/zh/doc-detail/39607.htm  example: https://BucketName.Endpoint/ObjectName
                return "https://" + publicBucketName
                        + "." + endpoint + "/" + saveFilePath;
            }
            return saveFilePath;

        } catch (Exception e) {
            log.error("error", e);
            return null;
        }
    }

    @Override
    public boolean downloadFile(OssSavePlaceEnum ossSavePlaceEnum, String source, String target) {
        try {
            String bucket = ossSavePlaceEnum == OssSavePlaceEnum.PRIVATE
                    ? privateBucketName :
                    publicBucketName;
            this.ossClient.getObject(new GetObjectRequest(bucket, source), new File(target));

            return true;
        } catch (Exception e) {
            log.error("error", e);
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
    }
}
